package com.qf.Dao.impl;

import com.qf.Dao.PatientInfoDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.PatientInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class PatientInfoDaoImpl implements PatientInfoDao {
    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    @Override
    public List<PatientInfo> selectPatientInfo(String name) throws SQLException {
        String sql = "select * from patient_info where patient_Name =? ";
        List<PatientInfo> patientInfo = qr.query(sql, new BeanListHandler<PatientInfo>(PatientInfo.class), name);
        return patientInfo;
    }

    @Override
    public int deletePatientInfo(String name) throws SQLException {
        String sql = "DELETE FROM patient_info  WHERE patient_Name = ?";
        Object obj = name;
        int update = qr.update(sql, obj);
        return update;
    }

    @Override
    public int addPatientInfo(PatientInfo patientInfo) throws SQLException {
        String sql = "INSERT INTO patient_info(patient_Idcard,patient_Name,patient_Money)VALUES(?,?,?)";
        Object[] objects = {patientInfo.getPatientIdcard(), patientInfo.getPatientName(), patientInfo.getPatientMoney()};
        int update = qr.update(sql, objects);
        return update;
    }

    @Override
    public int updatePatientInfo(PatientInfo patientInfo) throws SQLException {
        String sql = "UPDATE patient_info SET patient_Idcard= ?  , patient_Money= ? WHERE patient_Name= ? ";
        Object[] objects = {patientInfo.getPatientIdcard(), patientInfo.getPatientMoney(), patientInfo.getPatientName()};
        int update = qr.update(sql, objects);
        return update;
    }
}
